@ModelAttribute
、@ResponseBody
、 @RequestBody
和 @RequestParam
是 Spring 框架中用於處理Controller方法和請求主體的註解:
@Controller
public class MyController {
@RequestMapping("/hello")
public ModelAndView initHello(@ModelAttribute Model model) {
ModelAndView modelAndView = new ModelAndView("helloView");
modelAndView.addObject("message", model.message);
return modelAndView;
}
}
@Controller
public class MyController {
@RequestMapping("/hello")
@ResponseBody
public ModelResp sendHello(@RequestBody Model model) {
ModelResp modelResp = new ModelResp();
ModelResp.setMessage("message", model.message);
return modelResp;
}
}
@Controller
public class UserController {
@RequestMapping("/hello")
@ResponseBody
public ModelResp sayHello(@RequestBody Message message) {
ModelResp modelResp = new ModelResp();
ModelResp.setMessage("message", message.msg);
return modelResp;
}
}
@Controller
public class UserController {
@RequestMapping("/hello")
@ResponseBody
public ModelResp sayHello(@RequestParam("message") String message) {
ModelResp modelResp = new ModelResp();
ModelResp.setMessage("message", message);
return modelResp;
}
}
@ModelAttribute
用於在方法中添加默認用戶數據到模型,以供視圖使用。@ResponseBody
用於在方法中將用戶對象直接作為 JSON 響應發送給客戶端。@RequestBody
用於在方法中從請求主體中提取 JSON 數據進行處理。@RequestBody
用於在方法中從請求主體中提取參數數據進行處理。@ModelAttribute
。@ResponseBody
。@RequestBody
。@RequestParam
。https://docs.spring.io/spring-framework/reference/web/webflux/controller/ann-methods/requestbody.html
https://docs.spring.io/spring-framework/reference/web/webflux/controller/ann-methods/responsebody.html
https://docs.spring.io/spring-framework/reference/web/webflux/controller/ann-methods/modelattrib-method-args.html
https://docs.spring.io/spring-framework/reference/web/webflux/controller/ann-methods/requestparam.html